<template>
  <el-dialog
    width="24%"
    :title="`${showText}学科`"
    :visible="showDialog"
    @close="btnCancel"
  >
    <el-form ref="perForm" :model="perForm" :rules="rules">
      <el-form-item prop="subjectName" label="学科名称">
        <el-input
          v-model="perForm.subjectName"
          type="text"
          size="small"
          style="width: 70%"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否显示">
          <el-switch
          v-model="perForm.isFrontDisplay"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        />

      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button  @click="btnCancel">取 消</el-button>
      <el-button type="primary" @click="btnOK">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { update, add } from '@/api/hmmm/subjects.js'
export default {
  name: 'subjects-add',
  props: {
    showDialog: {
      type: Boolean,
      default: true
    },
    perForm: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      rules: {
        subjectName: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText () {
      return this.perForm.id ? '编辑' : '新增'
    }
  },
  methods: {
    btnCancel () {
      // 重置检验规则
      this.$emit('update:showDialog', false) // 关闭弹窗
      // this.$emit('currSubject', {})
      // this.$emit('currSubject.id', '')
      // this.$refs.perForm.resetFields()
      // this.perForm = {}
      this.$parent.currSubject = {}
      this.$refs.perForm.resetFields()
    },
    async btnOK () {
      try {
        await this.$refs.perForm.validate()
        if (this.perForm.id) {
          // 修改
          // console.log(this.perForm.isFrontDisplay)
          await update(this.perForm)
        } else {
          // 更新
          await add(this.perForm)
        }
        // 重新渲染界面
        this.$emit('getSubjects')
        this.$emit('currSubject.id', '')
        this.$emit('currSubject', {})
        this.$emit('update:showDialog', false)
        this.$message({
          message: '操作成功',
          type: 'success'
        })
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>

<style scoped lang='scss'></style>
